var express = require("express");
// const {json} = require("express/lib/response");
var router = express.Router();
const shopsService = require("../service/shopsService");

/**
 * @swagger
 * /shops:
 *    get:
 *      tags:
 *      - shops
 *      summary: 查找商品
 *     
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// //搜索 相对路径 我的
router.get("/", async function (req, res) {
  let {
    type,
    value,
    current,
    pageSize
  } = req.query;
  console.log(req.query);
  let searchObj = {}; //从页面得到的参数
  if (type) {

    searchObj[type] = { $regex: value }; //姓名模糊查询
    // if (type == 'typeName') {
    //   searchObj[type] = value.typeName;
    // }
  }
  // 如果页码有值就是current，没有就是1 每页有值就是pageSize 没有就为5
  let data = await shopsService.find(searchObj, {
    current: current || 1,
    pageSize: pageSize || 5,
  });
  // console.log(data);
  res.send({
    data
  });
});

// 查找所有
/**
 * @swagger
 * /shops/all:
 *    get:
 *      tags:
 *      - shops
 *      summary: 查所有商品
 *     
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get("/all", async function (req, res) {
  let data = await shopsService.findAll();
  res.send({
    data,
  });
});

/**
 * @swagger
 * /shops:
 *    post:
 *      tags:
 *      - shops
 *      summary: 增加商品
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:
 *                  type: string
 *                price:
 *                  type: string
 *                total:
 *                  type: string
 *                types:
 *                  type: string
 *                shopImg:
 *                  type: array
 *                isStatus:
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// 增加  
router.post("/", async function (req, res) {
  let { name, price, total, types, shopImg, isStatus } = req.body;
  // console.log(types,'后台');

  let data = await shopsService.insert({
    name,
    price,
    total,
    types,
    shopImg,
    isStatus
  });
  // if(Boolean.valueOf(data.isStatus)){
  //   code
  // }
  res.send({
    data,
  });
});

/**
 * @swagger
 * /shops/{id}/:
 *    delete:
 *      tags:
 *      - shops
 *      summary: 删除商品
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 商品id
 *        required: true          
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// 删除
router.delete("/:id", async function (req, res) {
  let {
    id
  } = req.params;
  console.log(req.params);
  let data = await shopsService.remove(id);
  res.send({
    data
  });
});

/**
 * @swagger
 * /shops/{id}:
 *    put:
 *      tags:
 *      - shops
 *      summary: 修改商品
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 商品id
 *        required: true
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:
 *                  type: string
 *                price:
 *                  type: string
 *                total:
 *                  type: string
 *                types:
 *                  type: string
 *                shopImg:
 *                  type: array
 *                isStatus:
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
//   // 查id 不加冒号就是写死的id
router.get("/:id", async function (req, res) {
  let {
    id
  } = req.params;
  console.log(req.query);
  let data = await shopsService.findById(id);
  res.send({
    data
  });
});

// 修改
router.put("/:id", async function (req, res) {

  let {
    id
  } = req.params;
  let {
    name, price, total, types, shopImg, isStatus
  } = req.body;
  let data = await shopsService.update(id, {
    name, price, total, types, shopImg, isStatus
  });
  res.send({
    data
  });
});

//   router.get('/films/:id', async function (req, res, next) {
//     res.send(await shopsService.findByFilmId(req.params.id));
//   });

module.exports = router;
